<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>力扣: 54.螺旋矩阵 | 园田家</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="力扣：54.螺旋矩阵今天的力扣每日一题是《螺旋矩阵》。这道题我在大一大二时都见过，但是每次做的时候都得重新思考如何做。每一次我都做不出来，做不出来就去网上找答案，找了答案也没认真深究过，所以也会忘得很快。今天做这道题的时候也是，一开始想了很久，结果实在想不出来了，最后去看题解了。题解里面有一个人写的方法我觉得非常好，很容易让我这种算法小白理解，因此我决定把它记下来。  题目描述：给你一个 m 行">
<meta property="og:type" content="article">
<meta property="og:title" content="力扣: 54.螺旋矩阵">
<meta property="og:url" content="http://example.com/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/index.html">
<meta property="og:site_name" content="园田家">
<meta property="og:description" content="力扣：54.螺旋矩阵今天的力扣每日一题是《螺旋矩阵》。这道题我在大一大二时都见过，但是每次做的时候都得重新思考如何做。每一次我都做不出来，做不出来就去网上找答案，找了答案也没认真深究过，所以也会忘得很快。今天做这道题的时候也是，一开始想了很久，结果实在想不出来了，最后去看题解了。题解里面有一个人写的方法我觉得非常好，很容易让我这种算法小白理解，因此我决定把它记下来。  题目描述：给你一个 m 行">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral1.jpg">
<meta property="og:image" content="http://example.com/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral2.jpg">
<meta property="article:published_time" content="2021-03-15T08:17:49.000Z">
<meta property="article:modified_time" content="2021-03-18T16:27:32.417Z">
<meta property="article:author" content="kurodasense">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral1.jpg">
  
    <link rel="alternate" href="/atom.xml" title="园田家" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/css/style.css">

<meta name="generator" content="Hexo 5.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">园田家</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="搜索"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-力扣-54-螺旋矩阵" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/" class="article-date">
  <time datetime="2021-03-15T08:17:49.000Z" itemprop="datePublished">2021-03-15</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      力扣: 54.螺旋矩阵
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="力扣：54-螺旋矩阵"><a href="#力扣：54-螺旋矩阵" class="headerlink" title="力扣：54.螺旋矩阵"></a>力扣：54.螺旋矩阵</h2><p>今天的力扣每日一题是《螺旋矩阵》。这道题我在大一大二时都见过，但是每次做的时候都得重新思考如何做。<br>每一次我都做不出来，做不出来就去网上找答案，找了答案也没认真深究过，所以也会忘得很快。<br>今天做这道题的时候也是，一开始想了很久，结果实在想不出来了，最后去看题解了。<br>题解里面有一个人写的方法我觉得非常好，很容易让我这种算法小白理解，因此我决定把它记下来。</p>
<hr>
<h3 id="题目描述：给你一个-m-行-n-列的矩阵-matrix-，请按照-顺时针螺旋顺序-，返回矩阵中的所有元素。"><a href="#题目描述：给你一个-m-行-n-列的矩阵-matrix-，请按照-顺时针螺旋顺序-，返回矩阵中的所有元素。" class="headerlink" title="题目描述：给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。"></a>题目描述：给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素。</h3><p><img src="/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral1.jpg" alt="示例1">  </p>
<pre><code>输入：matrix = [[1,2,3],[4,5,6],[7,8,9]]  
输出：[1,2,3,6,9,8,7,4,5]</code></pre>
<p><img src="/images/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/spiral2.jpg" alt="示例2"></p>
<pre><code>输入：matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]  
输出：[1,2,3,4,8,12,11,10,9,5,6,7]   </code></pre>
<p>提示：</p>
<ul>
<li>m == matrix.length</li>
<li>n == matrix[i].length</li>
<li>1 &lt;= m, n &lt;= 10</li>
<li>-100 &lt;= matrix[i][j] &lt;= 100  </li>
</ul>
<hr>
<p>我的解题：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> List&lt;Integer&gt; <span class="title">spiralOrder</span><span class="params">(<span class="keyword">int</span>[][] matrix)</span> </span>&#123;</span><br><span class="line">        List&lt;Integer&gt; list = <span class="keyword">new</span> ArrayList&lt;Integer&gt;();</span><br><span class="line">        <span class="keyword">int</span> m = matrix.length;</span><br><span class="line">        <span class="keyword">int</span> n = matrix[<span class="number">0</span>].length;</span><br><span class="line">        <span class="keyword">int</span> left=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> top=<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">int</span> right=n-<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> bottom=m-<span class="number">1</span>;</span><br><span class="line">        <span class="keyword">int</span> i=<span class="number">0</span>,j=<span class="number">0</span>; <span class="comment">//初始化遍历变量都为0，因为是从[0, 0]开始</span></span><br><span class="line">        <span class="keyword">while</span>(<span class="keyword">true</span>)&#123;</span><br><span class="line">            <span class="keyword">for</span>(j=left;j&lt;=right;j++) list.add(matrix[i][j]);</span><br><span class="line">            top++;</span><br><span class="line">            j=right;</span><br><span class="line">            <span class="keyword">if</span>(top&gt;bottom) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(i=top;i&lt;=bottom;i++) list.add(matrix[i][j]);</span><br><span class="line">            right--;</span><br><span class="line">            i=bottom;</span><br><span class="line">            <span class="keyword">if</span>(right&lt;left) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(j=right;j&gt;=left;j--) list.add(matrix[i][j]);</span><br><span class="line">            bottom--;</span><br><span class="line">            j=left;</span><br><span class="line">            <span class="keyword">if</span>(top&gt;bottom) <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">for</span>(i=bottom;i&gt;=top;i--) list.add(matrix[i][j]);</span><br><span class="line">            left++;</span><br><span class="line">            i=top;</span><br><span class="line">            <span class="keyword">if</span>(left&gt;right) <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> list;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们要把一个矩阵螺旋地输出出来，也就是按照顺时针的方向遍历矩阵。<br>我们可以创建四个指针：<code>top, bottom, left, right</code>。并且再设置两个变量<code>i, j</code>来分别负责遍历从<code>top</code>到<code>bottom</code>和从<code>left</code>到<code>right</code>的矩阵。<br>其中，<code>初始化top = 0, bottom = m - 1, left = 0, right = n - 1</code>。<br>螺旋矩阵，即从外到内，遍历每一个元素。从外到内的体现是：遍历了一边的元素后，该边就要向内缩小。<br>比如：一开始<code>top = 0</code>指代的是第一行，那么遍历完第一行的元素后，<code>top++</code>就是让它往内部移动，<br>而<code>bottom--</code>的意思也是如此：第一次遍历完第<code>m-1</code>行后，就往内部移动。<code>left++</code>和<code>right--</code>也是如此。<br>因此，我们可以得到一个遍历结束的条件：当上下边或左右边交叠的时候，就意味着螺旋矩阵已经形成。这体现在<br><code>if(left&gt;right) break;</code>和<code>if(top&gt;bottom) break;</code>，当交叠的时候，就跳出<code>while(true)</code>的循环，然后输出存储螺旋矩阵的<code>list</code>。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/" data-id="ckmadc8uz0000ncv5g6l17ban" class="article-share-link">Share</a>
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          力扣-115-不同的子序列
        
      </div>
    </a>
  
  
    <a href="/2020/11/30/hello-world/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">Hello World</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">归档</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/04/">四月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/03/">三月 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/11/">十一月 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">最新文章</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2021/04/18/leetcode-26/">leetcode-26</a>
          </li>
        
          <li>
            <a href="/2021/03/21/hasNext-%E4%B8%8EhasNextLine/">hasNext()与hasNextLine()</a>
          </li>
        
          <li>
            <a href="/2021/03/18/%E5%8A%9B%E6%89%A3-92-%E5%8F%8D%E8%BD%AC%E9%93%BE%E8%A1%A8II/">力扣-92-反转链表II</a>
          </li>
        
          <li>
            <a href="/2021/03/17/%E5%8A%9B%E6%89%A3-115-%E4%B8%8D%E5%90%8C%E7%9A%84%E5%AD%90%E5%BA%8F%E5%88%97/">力扣-115-不同的子序列</a>
          </li>
        
          <li>
            <a href="/2021/03/15/%E5%8A%9B%E6%89%A3-54-%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5/">力扣: 54.螺旋矩阵</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2021 kurodasense<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>




  </div>
</body>
</html>